import http from 'http'
import wx from 'weixin-js-sdk'

var wx_vue = {
	init : async function() {
		/*
		var res = await http.m_post("/api/weixin_config", {"weixin_url": location.href});
		wx.config(res);
		*/
	},

	share : async function(desc, imgurl) {
		console.log("share");
		var res = await http.m_post("/api/weixin_config", {"weixin_url": location.href});
		wx.config(res);

		wx.ready(function(){

			wx.onMenuShareAppMessage({
                    title: "们搭搭",
                    desc: desc,
                    link: location.href,
                    imgUrl: imgurl,
                    type: 'link',
                    dataUrl: '',
                    success: function () { 
                    },
                    cancel: function () { 

                    }
            });	
		});
		
	},
	image : async function(cb, picture_data) {
		var res = await http.m_post("/api/weixin_config", {"weixin_url": location.href});
		wx.config(res);

		wx.chooseImage({
			count: 6,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function (res) {
				var localIds = res.localIds; 
                var serverids = [];
  
                for (var i = 0; i < localIds.length; i++) {
  
                    wx.uploadImage({
                        localId: localIds[i], // 需要上传的图片的本地ID，由chooseImage接口获得
                        isShowProgressTips: 1, // 默认为1，显示进度提示
                        success: function (res) {
                            var serverId = res.serverId; // 返回图片的服务器端ID
                            serverids.push(serverId);

							http.m_post("/api/blog/new", {"picture_list": [serverId]});
  
                            if (serverids.length == localIds.length) {
                                cb(serverids);
                            }
                        }
                    });
					picture_data.push(localIds[i]);
					/*
					wx.getLocalImgData({
						localId: localIds[i],
						success: function (res) {
							var localData = res.localData;
							picture_data.push(localData);
						}
					});
					*/
				}
                
			}
		});
	}
}

export default wx_vue;


